package priv.liaohaolong.fullstacktest.controller;

import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MyErrorController implements ErrorController {

    public static final Logger logger = LoggerFactory.getLogger(MyErrorController.class);

    @RequestMapping("/error")
    public String handleError(HttpServletRequest request) {
        logger.error("Error: {}", request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE));

        if (request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE).equals(403))
            return "redirect:/forbidden";
        return "forward:/index.html";
    }

}
